﻿using System.Web.Mvc;
using nsadv.Statistic.Domain;
using nsadv.Statistic.Web.ViewModels;

namespace nsadv.Statistic.Web.Controllers
{
    public class AccountController : BaseController
    {
        public ActionResult Logon()
        {
			var model = CreateModel<LogonModel>();

			model.ShowLoginButton = false;

			model.UserName = "Логин";
			model.Password = "Пароль";

			return View(model);
        }

		[HttpPost]
		public ActionResult Logon(LogonModel model, string backUrl = null)
		{
			InitModel(model);

			if (ModelState.IsValid)
			{
				User user;
				if (!Membership.ValidateUser(model.UserName, model.Password, out user))
				{
					ModelState.AddModelError("", "Неправильное имя пользователя или пароль");
					return View(model);
				}

				Authehtication.SignIn(user.Id, model.Remember);

				if (!string.IsNullOrEmpty(backUrl))
					return Redirect(backUrl);

				return RedirectToAction("Cabinet", "Statistic");
			}

			return View(model);
		}
    }
}
